package com.lcf.app;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.process.ffmpeg.DefaultFFMPEGLocator;
import ws.schild.jave.progress.EncoderProgressListener;

import java.io.File;
import java.io.IOException;
import java.util.Map;

public class Demo {

    public static void main(String[] args) throws IOException, EncoderException {
        TimeInterval timer = DateUtil.timer();
        /*
        long l = System.currentTimeMillis();
        String input = "C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_bij0qf56w0ib21\\FileStorage\\File\\2023-08\\0c10982d96771ec38f3e6b3cbf5fe89a_raw-0001.MPG";

        try (ProcessWrapper ffmpeg = new DefaultFFMPEGLocator().createExecutor();){
            ffmpeg.addArgument("-i");
            ffmpeg.addArgument(input);
            ffmpeg.addArgument("-c:v");
            ffmpeg.addArgument("libx264");
            //ffmpeg.addArgument("-crf");
            //ffmpeg.addArgument("19");
            //ffmpeg.addArgument("-strict");
            //ffmpeg.addArgument("experimental");
            ffmpeg.addArgument("F:/" + "convert.mp4");
            ffmpeg.addArgument("-y");
            ffmpeg.execute();
            try (BufferedReader br = new BufferedReader(new InputStreamReader(ffmpeg.getErrorStream()))) {
                blockFfmpeg(br);
            }

        }*/

        MultimediaObject multimediaObject = new MultimediaObject(new File("F:\\CloudMusic\\Esan - 得到你的人却得不到你的心.mp3"));

        MultimediaInfo info = multimediaObject.getInfo();
        Map<String, String> metadata =
                info.getMetadata();
        System.out.println(info);


        String[] videoEncoders = new Encoder().getAudioEncoders();
        String[] supportedEncodingFormats = new Encoder().getSupportedEncodingFormats();
        System.out.println(String.join(" ; ", supportedEncodingFormats));
        System.out.println(String.join(" ; ", videoEncoders));

        EncodingAttributes encodingAttributes = new EncodingAttributes();
        encodingAttributes.setOutputFormat("mp4");
        VideoAttributes videoAttributes = new VideoAttributes();
        videoAttributes.setCodec("libx264");
        encodingAttributes.setVideoAttributes(videoAttributes);
        encodingAttributes.setAudioAttributes(new AudioAttributes());

        Encoder encoder = new Encoder();
        encoder.encode(
                new MultimediaObject(new File("F:\\迅雷下载\\阳光电影dy.ygdy8.com.疾速追杀4.2023.HD.1080P.中英双字.mkv"))
                , new File("F:/toMp4.mp4"), encodingAttributes, new EncoderProgressListener() {
                    @Override
                    public void sourceInfo(MultimediaInfo info) {
                        System.out.println("info: " + info);
                    }

                    @Override
                    public void progress(int permil) {
                        System.out.println("permil: " + permil);
                    }

                    @Override
                    public void message(String message) {
                        System.out.println("message: " + message);
                    }
                });
        Console.log("耗时：{}/秒", timer.intervalSecond());

        DefaultFFMPEGLocator locator= new DefaultFFMPEGLocator();
        String exePath= locator.getExecutablePath();
        System.out.println("ffmpeg executable found in <"+exePath+">");


    }

    private static void doNothing(String line) {
        System.out.println(line);
    }

}
